package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Music;
import com.qf.service.Impl.MusicServiceImpl;
import com.qf.service.MusicService;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author yangl
 * @version 1.0
 * @date 2023/4/24 14:49
 */

@WebServlet("/musicUploadServlet.action")
@MultipartConfig
public class MusicUploadServlet extends HttpServlet {

    //设置歌曲图片的访问路径
    private String musicPath;

    private MusicService musicService = new MusicServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传递的参数
        String param = req.getParameter("param");
        //判断
        if(param != null){
            if("upload".equals(param)){
                upload(req,resp);
            }else if("add".equals(param)){
                add(req,resp);
            }
        }else{
            throw new RuntimeException("param不能为空");
        }

    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("添加歌曲");
        //接收参数
        String musicName = req.getParameter("musicName");
        String musicAlbumName = req.getParameter("musicAlbumName");
        String musicMp3url = req.getParameter("musicMp3url");
        String musicArtistName = req.getParameter("musicArtistName");
        String sheetId = req.getParameter("sheetId");

        //封装对象
        Music music = new Music();
        music.setMusicName(musicName);
        music.setMusicAlbumName(musicAlbumName);
        music.setMusicAlbumPicurl(musicPath);
        music.setMusicMp3url(musicMp3url);
        music.setMusicArtistName(musicArtistName);
        music.setSheetId(sheetId);

        //调用业务逻辑层方法
        musicService.add(music);

        //重定向
        resp.sendRedirect(req.getContextPath() + "/home.html");

    }

    protected void upload(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取上传的文件，layui中默认上传文件的属性名为：file
        Part file = req.getPart("file");
        //获取上传的文件名
        String fileName = file.getSubmittedFileName();
        //保证文件名的唯一性（UUID）
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //拼接
        String uploadFileName = uuid + fileName;
        //指定上传的路径
        String path = "C:\\Users\\阿亘\\Desktop\\apache-tomcat-9.0.73\\webapps\\upload\\";
        //上传
        file.write(path + uploadFileName);
        //返回数据给前端，设置图片的访问路径
        musicPath = "http://localhost:8083/upload/" + uploadFileName;
        //返回前端UI框架的指定格式
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("data",musicPath);

        String jsonMap = JSON.toJSONString(map);
        resp.getWriter().write(jsonMap);
    }

}
